#include<cstdio>
#include<cstring>
#include <algorithm>

using namespace std;

int nums[2020];
int dp[2020][2020];

int main(){
    int n;
    while(scanf("%d", &n) && n){
        for(int i = 1; i <= n; i++){
            scanf("%d", &nums[i]);
            nums[n + i] = nums[i]; 
        }

        memset(dp, 0, sizeof(dp));

        for(int i = 2 * n; i >= 1; i--){
            for(int j = i + 1; j <= 2 * n; j++){
                dp[i][i] = 1;
                if(nums[i] == nums[j]){
                    dp[i][j] = dp[i + 1][j - 1] + 2;
                }else{
                    dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
                }
            }
        }

        int res = 0;
        for(int i = 1; i <= n; i++)
            res = max(res, dp[i][i + n - 1]);

        for(int i = 1; i <= n; i++)
            res = max(res, dp[i][i + n - 2] + 1);

        printf("%d\n", res);
    }
}